package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.BancoDao;
import edu.upc.clase.demo.entity.Banco;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Carlos Ríos Rosales
 */
@Repository
public class BancoDaoImpl extends SimpleJdbcDaoSupport implements BancoDao {

  private static Logger log = LoggerFactory.getLogger(BancoDaoImpl.class);

  @Autowired
  public BancoDaoImpl(DataSource dataSource) {
    log.info("Asignando el dataSource");
    setDataSource(dataSource);
  }

  @Override
  public Integer insertar(Banco banco) {
    getJdbcTemplate().update(
            "Insert into banco (codigo, descripcion, flag) Values (?, ?, ?)",
            banco.getCodigo(), banco.getDescripcion(), banco.isFlag());
    return getSimpleJdbcTemplate().queryForInt("select last_insert_id()");
  }

  @Override
  public void actualizar(Banco banco) {
    getJdbcTemplate().update(
            "update banco set codigo = ?, descripcion = ?, flag = ? Where IdBanco = ?",
            banco.getCodigo(), banco.getDescripcion(), banco.isFlag(), banco.getIdBanco());
  }

  @Override
  public void eliminar(Banco banco) {
    getJdbcTemplate().update("delete from banco where idbanco = ?", banco.getIdBanco());
  }

  @Override
  public List<Banco> buscarTodos() {
    return getSimpleJdbcTemplate().query("Select * from banco",
            new BeanPropertyRowMapper<Banco>(Banco.class));
  }

  @Override
  public Banco buscarCodigo(String codigo) {
    try {
      return getSimpleJdbcTemplate().queryForObject("select * from banco where codigo = ?",
              new BeanPropertyRowMapper<Banco>(Banco.class), codigo);
    } catch (EmptyResultDataAccessException e) {
      return null;
    }
  }

  @Override
  public Banco buscarIdBanco(Integer idBanco) {
    try {
      return getSimpleJdbcTemplate().queryForObject(
              "select * from banco where idbanco = ?",
              new BeanPropertyRowMapper<Banco>(Banco.class), idBanco);
    } catch (EmptyResultDataAccessException e) {
      return null;
    }
  }

  @Override
  public List<Banco> buscarPorNombre(String nombre) {
    try {
      Map<String, String> parametros = new HashMap<String, String>();
      parametros.put("descripcion", "%" + nombre + "%");
      return getSimpleJdbcTemplate().query(
              "select * from banco where descripcion like :nombre",
              new BeanPropertyRowMapper<Banco>(Banco.class), parametros);
    } catch (EmptyResultDataAccessException e) {
      return null;
    }
  }
}